package top.anka1.ims;

import top.anka1.ims.BouncingShapes.bouncebox.*;
import top.anka1.ims.BouncingShapes.bouncebox.Rectangle;
import top.anka1.ims.BouncingShapes.bounceboxframework.Shape;

import java.awt.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class ShapeService {
    ArrayList<Shape> parseFile(String filePath) throws FileNotFoundException {
        FileInputStream fileIn = new FileInputStream(filePath);
        Scanner sc = new Scanner(fileIn);
        ArrayList<Shape> shapes = new ArrayList<>();
        while (sc.hasNextLine()) {
            Scanner line = new Scanner(sc.nextLine());
            String shapeType;
            if (line.hasNext()) {
                shapeType = line.next();
            } else {
                continue;
            }
            if (shapeType.equalsIgnoreCase("Circle")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int r = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Circle c1 = new Circle(x, y, r);
                        c1.setColor(new Color(red, green, blue));
                        c1.setVelocity(vx, vy);
                        shapes.add(c1);
                        //Circle created with velocity and color
                    } else {
                        Circle c1 = new Circle(x, y, r);
                        c1.setVelocity(vx, vy);
                        c1.setColor(Color.BLACK);
                        shapes.add(c1);
                        //Circle created with velocity
                    }
                } else {
                    Circle c1 = new Circle(x, y, r);
                    c1.setColor(Color.BLACK);
                    shapes.add(c1);
                    //Circle created without velocity
                }
            } else if (shapeType.equalsIgnoreCase("Square")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int side = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Square s1 = new Square(x, y, side);
                        s1.setColor(new Color(red, green, blue));
                        s1.setVelocity(vx, vy);
                        shapes.add(s1);
                        //Square created with velocity and color
                    } else {
                        Square s1 = new Square(x, y, side);
                        s1.setVelocity(vx, vy);
                        s1.setColor(Color.BLACK);
                        shapes.add(s1);
                        //Square created with velocity
                    }
                } else {
                    Square s1 = new Square(x, y, side);
                    s1.setColor(Color.BLACK);
                    shapes.add(s1);
                    //Square created without velocity
                }
            } else if (shapeType.equalsIgnoreCase("Triangle")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int base = line.nextInt();
                int height = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Triangle t1 = new Triangle(x, y, base, height);
                        t1.setColor(new Color(red, green, blue));
                        t1.setVelocity(vx, vy);
                        shapes.add(t1);
                        //Triangle created with velocity and color
                    } else {
                        Triangle t1 = new Triangle(x, y, base, height);
                        t1.setVelocity(vx, vy);
                        t1.setColor(Color.BLACK);
                        shapes.add(t1);
                        //Triangle created with velocity
                    }
                } else {
                    Triangle t1 = new Triangle(x, y, base, height);
                    t1.setColor(Color.BLACK);
                    shapes.add(t1);
                    //Triangle created without velocity
                }
            } else if (shapeType.equalsIgnoreCase("Rectangle")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int base = line.nextInt();
                int height = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Rectangle r1 = new Rectangle(x, y, base, height);
                        r1.setColor(new Color(red, green, blue));
                        r1.setVelocity(vx, vy);
                        shapes.add(r1);
                        //Rectangle created with velocity and color
                    } else {
                        Rectangle r1 = new Rectangle(x, y, base, height);
                        r1.setVelocity(vx, vy);
                        r1.setColor(Color.BLACK);
                        shapes.add(r1);
                        //Rectangle created with velocity
                    }
                } else {
                    Rectangle r1 = new Rectangle(x, y, base, height);
                    r1.setColor(Color.BLACK);
                    shapes.add(r1);
                    //Rectangle created without velocity
                }
            } else {
                System.err.println(shapeType + " is not a recognized shape");
            }
        }
        /*
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                String shapeType = parts[0].trim();
                int x = Integer.parseInt(parts[1].trim());
                int y = Integer.parseInt(parts[2].trim());
                if (shapeType.equals("Circle")) {
                    int radius = Integer.parseInt(parts[3].trim());
                    shapes.add(new Circle(x, y, radius));
                } else if (shapeType.equals("Rectangle")) {
                    int width = Integer.parseInt(parts[3].trim());
                    int height = Integer.parseInt(parts[4].trim());
                    shapes.add(new Rectangle(x, y, width, height));
                } else if (shapeType.equals("Triangle")) {
                    int base = Integer.parseInt(parts[3].trim());
                    int height = Integer.parseInt(parts[4].trim());
                    shapes.add(new Triangle(x, y, base, height));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
         */
        return shapes;
    }
    public HashMap<String, Integer> countByType(ArrayList<Shape> shapes) {
        HashMap<String, Integer> shapeCount = new HashMap<>();
        for (Shape shape : shapes) {
            String className = shape.getClass().getSimpleName();
            shapeCount.put(className, shapeCount.getOrDefault(className, 0) + 1);
        }
        return shapeCount;
    }
    public String hashmapToString(HashMap<String, Integer> map) {
        StringBuilder sb = new StringBuilder();
        for (String key : map.keySet()) {
            sb.append(key).append(": ").append(map.get(key)).append("\n");
        }
        return sb.toString();
    }
    public double totalArea(ArrayList <Shape> shapes) {
        double totalArea = 0;
        for (Shape shape : shapes) {
            totalArea += shape.getMass();
        }
        return totalArea;
    }
    public void displayShapes(ArrayList<Shape> shapes) {
        BounceBox box = new BounceBox(700, 500);
        for (Shape shape : shapes) {
            box.addShape(shape);
        }
        box.start();
    }
}
